var _GLState = "";
var _GLTurn;

function GLLogic(){
	switch(_GLState){
		case "开始阶段":{
			console.log("回合开始");
			_GDNowChess.aiCounter++;
			_GEEventList.unshift(new GEEvent("开始",_GDNowChess));
			_GLState = "寻求响应";
			break;
		}
		case "等待处理":{
			break;
		}
		case "寻求响应":{
			_GLState = "等待处理";
			GEAskForResponse();
			break;
		}
		case "事件执行":{
			if(!_GEEventing)
				GEDoEvent();
			break;
		}
		case "等待操作":{
			if((_GDAttackDone||_GDNowChess == null)&&!_GEEventList.length){
				_GEEventList.push(new GEEvent("结束",null));
				_GLState = "寻求响应";
				break;
			}
			if(_GDNowChess.ai != undefined && _GDNowChess.ai != "")
				AIGo();
			break;
		}
		case "动画运行":{
			if(_GADone)
				if(_GAList.length != 0)
					GAPlayAction();
				else
					GEAskForResponse();
			break;
		}
	}
};

function GLReloading(){
	_GLState = "";
};

function GLNewGame(){
	if(getSettingData("skipAll") == "false"){
		setStartData(_level);
	}
	for(var i=0;i<_battle[_level].length;i++){
		var string = _battle[_level][i].position.split(",");
		var position = new GDposition(string[0],string[1]);
		_board[position.x][position.y] = ChessLoader(_battle[_level][i].chess,_battle[_level][i].team,_battle[_level][i].ai,_battle[_level][i].boss,position);
		g_GBTouchLayer.addChild(_board[position.x][position.y].sprite);
		// 插入棋子列表
		_GDChessList.push(_board[position.x][position.y]);
	}
	// 插入技能精灵
	_GUISkill = new Array();
	for(var i = 0;i<6;i++){
		_GUISkill.push(new SkillSprite(i));
		g_GBTouchLayer.addChild(_GUISkill[i]);
	}
	_GUIMenu = new Array();
	for(var i = 0;i<4;i++){
		_GUIMenu.push(new SkillSprite(i));
		g_GBTouchLayer.addChild(_GUIMenu[i]);
	}
	_GLTurn = 0;
	newHPMP();
	GLNewTurn();
};

function GLNewTurn(){
	_GLState = "等待处理";
	_GLTurn ++;
	GAShowNowTurn();
};

function GLStartTurn(){
	GUICleanAllTag();
	_GDThisTurnNoMoveList = _GDChessList.concat();
	_GDThisTurnDelayList = new Array();
	GLNextChess();
}

function GLEndStep(){
	GUISetTagEnd(_GDNowChess);
	GLNextChess();
};

function GLDelay(){
	GUISetTagDelay(_GDNowChess);
	_GDThisTurnDelayList.unshift(_GDNowChess);
	GLNextChess();
};

function GLNextChess(){
	var next;
	if(_GDThisTurnNoMoveList.length){
		next = GLNextFastest(_GDThisTurnNoMoveList);
		_GDNowChess = _GDThisTurnNoMoveList[next];
		_GDThisTurnNoMoveList.splice(next,1);
	}
	else if(_GDThisTurnDelayList.length){
		next = GLNextSlowest(_GDThisTurnDelayList);
		_GDNowChess = _GDThisTurnDelayList[next];
		GUICleanTag(_GDNowChess);
		_GDThisTurnDelayList.splice(next,1);
	}
	else{
		GLNewTurn();
		return;
	}
	GUIShowSkillIcon(_GDNowChess);
	_GLState = "开始阶段";
	_GDMoveDone = false;
	_GDAttackDone = false;
};

function GLNextFastest(list){
	var num = new Array();
	num.push(0);
	for(var i=1;i<list.length;i++){
		if(CSgetState("速度",list[num[0]].id)<CSgetState("速度",list[i].id)){
			var num = new Array();
			num.push(i);
		}
		else if(CSgetState("速度",list[num[0]].id)==CSgetState("速度",list[i].id)){
			num.push(i);
		}
	}
	return num[parseInt((num.length)*Math.random())];
};

function GLNextSlowest(list){
	var num = new Array();
	num.push(0);
	for(var i=1;i<list.length;i++){
		if(CSgetState("速度",list[num[0]].id)>CSgetState("速度",list[i].id)){
			var num = new Array();
			num.push(i);
		}
		else if(CSgetState("速度",list[num[0]].id)==CSgetState("速度",list[i].id)){
			num.push(i);
		}
	}
	return num[parseInt((num.length)*Math.random())];
};

function GLWinListener(){
	var ally = 0;
	var enemy = 0;
	for(var i in _GDChessList){
		if(_GDChessList[i].team == 0)
			ally++;
		if(_GDChessList[i].team == 1)
			enemy++;
	}
	if(ally == 0){
		_GLState = "等待处理";
		_GDFail = true;
		g_GBMessageLayer = new GBMessageLayer(_message[_level]);
		g_GBTouchLayer.addChild(g_GBMessageLayer,1000);
		g_GBMessageLayer._messageList=new Array();
		g_GBMessageLayer._messageList.push(new MMMessage (1,"res/head_speech.png",3,0,"貌似你们已经没有能战斗的队员了。"));
		g_GBMessageLayer._messageList.push(new MMMessage (1,"res/head_speech.png",3,0,"那就只好下次再来过了。"));
		if(getSettingData("ignoreFail") == "true"){
			var level = getLevelData();
			if(parseInt(level) <= parseInt(_level))
				setLevelData(parseInt(_level)+1);
			g_GBMessageLayer._messageList.push(new MMMessage (1,"res/head_speech.png",4,0,"………………"));
			g_GBMessageLayer._messageList.push(new MMMessage (1,"res/head_speech.png",0,0,"好吧好吧，那么就跳过这一关吧。"));
		}else{
			g_GBMessageLayer._messageList.push(new MMMessage (1,"res/head_speech.png",4,0,"别这么看着我，我是不会放水的。"));
		}
		g_GBMessageLayer.nextMessage();
		return true;
	}
	if(enemy == 0){
		_GLState = "等待处理";
		_GDWin = true;
		var level = getLevelData();
		if(parseInt(level) <= parseInt(_level))
			setLevelData(parseInt(_level)+1);
		setLevelDone(_level);
		g_GBMessageLayer = new GBMessageLayer(_message[_level]);
		g_GBTouchLayer.addChild(g_GBMessageLayer,1000);
		g_GBMessageLayer._messageList=new Array();
		if(getSettingData("skipRead") == "true" && getEndData(_level) == "done"){
			g_GBMessageLayer._messageList.push(new MMMessage (1,"res/head_speech.png",0,0,"既然这段剧情已经看过了，那么跳过也无所谓了。"));
		}
		else if(getSettingData("skipAll") == "true"){
			g_GBMessageLayer._messageList.push(new MMMessage (1,"res/head_speech.png",0,0,"好吧好吧，字太多不看的话，那就跳过吧。"));
		}
		else{
			setEndData(_level);
			g_GBMessageLayer._messageList = _end[_level].concat();
		}
		g_GBMessageLayer.nextMessage();
		return true;
	}
	return false;
};